<!DOCTYPE html>
<!--
Copyright (c) 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/extras/chrome/layout_object.html">

<script>
'use strict';

tr.b.unittest.testSuite(function() {
  function FakeSnapshot() {
    this.layoutObjectsById = {};
  }
  FakeSnapshot.prototype = {
    getLayoutObjectById(id) {
      return this.layoutObjectsById[id];
    }
  };

  test('instantiate', function() {
    const snapshot = new FakeSnapshot();
    const layoutObject = new tr.e.chrome.LayoutObject(snapshot, {
      address: 'deadbeef',
      name: 'LayoutView',
      other: 1,
      children: [
        {
          address: 'beef2',
          name: 'LayoutBlockFlow',
          other: 2
        }
      ]
    });
    snapshot.layoutObjectsById[layoutObject.id] = layoutObject;

    assert.strictEqual(snapshot, layoutObject.snapshot);
    assert.strictEqual('LayoutView', layoutObject.name);
    assert.strictEqual('deadbeef', layoutObject.id);
    assert.strictEqual(1, layoutObject.otherPropertyNames.length);
    assert.strictEqual(1, layoutObject.getProperty('other'));
    assert.isTrue(layoutObject.hasChildLayoutObjects);
    const child = layoutObject.childLayoutObjects[0];
    assert.strictEqual('LayoutBlockFlow', child.name);
    assert.strictEqual('beef2', child.id);
    assert.strictEqual(1, child.otherPropertyNames.length);
    assert.strictEqual(2, child.getProperty('other'));

    assert.isUndefined(layoutObject.previousSnapshotLayoutObject);
    assert.isUndefined(layoutObject.nextSnapshotLayoutObject);

    let count = 0;
    layoutObject.traverseTree(function(node) {
      count += 1;
    });
    assert.strictEqual(2, count);

    const nextSnapshot = new FakeSnapshot();
    nextSnapshot.previousSnapshot = snapshot;
    snapshot.nextSnapshot = nextSnapshot;

    const nextLayoutObject = new tr.e.chrome.LayoutObject(nextSnapshot, {
      address: 'deadbeef',
      name: 'LayoutView'
    });
    nextSnapshot.layoutObjectsById[nextLayoutObject.id] = nextLayoutObject;

    assert.strictEqual(layoutObject,
        nextLayoutObject.previousSnapshotLayoutObject);
    assert.strictEqual(nextLayoutObject, layoutObject.nextSnapshotLayoutObject);
    assert.isUndefined(layoutObject.previousSnapshotLayoutObject);
    assert.isUndefined(nextLayoutObject.nextSnapshotLayoutObject);
  });
});
</script>
